package com.genghis.ptas.inbox.service.impl;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.inbox.dao.InboxDao;
import com.genghis.ptas.inbox.entity.Inbox;
import com.genghis.ptas.inbox.service.InboxService;
import com.genghis.ptas.security.dao.UserDao;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.entity.UserBaseInfo;
import com.genghis.ptas.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("inboxService")
public class InboxServiceImpl implements InboxService {
    @Autowired
    private InboxDao inboxDao;

    @Autowired
    private UserDao userDao;

    @Override
    public int addInbox(Inbox inbox) {
        inbox.setSendDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        return inboxDao.addInbox(inbox);
    }

    @Override
    public void delInbox(String ids) {
        inboxDao.delInbox(ids);
    }

    @Override
    public List<Inbox> listAllInbox(HttpServletRequest request, Inbox inbox) {
        User user = (User) request.getSession().getAttribute("user");
        inbox.setUserId(user.getId());
        List<Inbox> inboxList = inboxDao.listAllInbox(inbox);
        for (Inbox inboxTemp : inboxList) {
            int userId = inboxTemp.getAddresseeId();
            if (userId > 0) {
                UserBaseInfo userBaseInfo = userDao.findUserBaseInfoByUserId(inboxTemp.getUserId());
                User userTemp = userDao.findUserAllInfoById(inboxTemp.getUserId());
                userTemp.setUserBaseInfo(userBaseInfo);
                inboxTemp.setUser(userTemp);
            } else {
                UserBaseInfo userBaseInfo = new UserBaseInfo();
                userBaseInfo.setPhotoUrl(DictConstant.DEFAULT_USER_PHOTO_URL);
                User userTemp = new User();
                userTemp.setUserName(DictConstant.ROLE_SYSTEM);
                userTemp.setUserBaseInfo(userBaseInfo);
                inboxTemp.setUser(user);
            }
        }
        return inboxList;
    }

    @Override
    public Inbox findInboxById(int id) {
        Inbox inbox = inboxDao.findInboxById(id);
        int userId = inbox.getAddresseeId();
        if (0 != userId) {
            User user = userDao.findUserAllInfoById(userId);
            UserBaseInfo userBaseInfo = userDao.findUserBaseInfoByUserId(userId);
            user.setUserBaseInfo(userBaseInfo);
            inbox.setUser(user);
        } else {
            User user = new User();
            user.setUserName(DictConstant.ROLE_SYSTEM);
            UserBaseInfo userBaseInfo = new UserBaseInfo();
            userBaseInfo.setPhotoUrl(DictConstant.DEFAULT_USER_PHOTO_URL);
            userBaseInfo.setEmail("");
            user.setUserBaseInfo(userBaseInfo);
            inbox.setUser(user);
        }
        return inbox;
    }

    @Override
    public void changeMassageState(String state, String ids) {
        inboxDao.changeMassageState(state, ids);
    }

    @Override
    public int countInboxCategory(Inbox inbox) {
        return inboxDao.listAllInbox(inbox).size();
    }

    @Override
    public void changeMassageCategory(String category, String ids) {
        inboxDao.changeMassageCategory(category, ids);
    }

    @Override
    public void delMassage(String ids) {
        inboxDao.delMassage(ids);
    }

    @Override
    public void addSentInbox(Inbox inbox) {
        inbox.setSendDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        inboxDao.addInbox(inbox);
        inbox.setUserId(inbox.getAddresseeId());
        inbox.setAddresseeId(inbox.getUser().getId());
        inbox.setState(DictConstant.INBOX_STATE_0);
        inbox.setCategory(DictConstant.INBOX_CATEGORY_1);
        inboxDao.addInbox(inbox);
    }


}
